package cn.edu.swu.miniwebsvr.core;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

public class Parameter {
    private String method;
    private String url;
    private Map<String, String> urlParams = new HashMap<>();
    private Map<String, String> bodyParams = new HashMap<>();
    private Map<String, String> headers = new HashMap<>();
    //合并视图缓存
    private Map<String, String> allparams;

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map<String, String> getUrlParams() {
        return urlParams;
    }

    public Map<String, String> getBodyParams() {
        return bodyParams;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    //按优先级获取单个参数（先body再url）
    public String getParam(String name) {
        if (bodyParams.containsKey(name))
            return bodyParams.get(name);
        return urlParams.get(name);
    }

    // TODO: [222024321072031/黄璜] 补充必要的构造函数、setter、getter方法
    public Map<String,String> getAllParams(){
        if (allparams == null){
            Map<String,String> merged = new LinkedHashMap<>();
            //URL参数优先装入，后面的body可覆盖
            merged.putAll(urlParams);
           merged.putAll(bodyParams);
            allparams = Collections.unmodifiableMap(merged);
        }
        return allparams;
    }
}